import axios from 'axios'
import { getToken } from '@/utils/auth';
import { Message, MessageBox, Notification } from "element-ui";
import store from '@/store';
import errorCode from '@/utils/errorCode';

export function downloadFile(fileName, filePath) {
  axios({
    baseURL: process.env.VUE_APP_BASE_API,
    method: 'get',
    url: '/file/download',
    responseType: 'blob',
    headers: {
      'Authorization': 'Bearer ' + getToken()
    },
    params: {
      fileName,
      filePath
    }
  }).then(res => {
    const blob = res.data;
    console.log(blob);
    let dom = document.createElement('a');
    let url = window.URL.createObjectURL(blob);
    dom.href = url;
    dom.download = decodeURI(fileName);
    dom.style.display = 'none';
    document.body.appendChild(dom);
    dom.click();
    dom.parentNode.removeChild(dom);
    window.URL.revokeObjectURL(url);
  }).catch(err => {
    let { message } = err;
    if (process.env.NODE_ENV === 'development') {
      Message({
        message: message,
        type: 'error',
        duration: 5 * 1000
      });
    }
    console.error(err.config.url, message);
  })
}
